package eu.dominicum.ra.db4o.outbound;

import javax.resource.ResourceException;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;

import org.apache.log4j.Logger;

public class Db4oConnectionManager implements ConnectionManager {

	private static final long serialVersionUID = 1L;
	private static Logger LOG = Logger.getLogger(Db4oConnectionManager.class);

	/**
	 * The method allocateConnection is called by the resource adapter's
	 * connection factory instance. This lets the connection factory instance
	 * provided by the resource adapter pass a connection request to the
	 * ConnectionManager instance. The connectionRequestInfo parameter
	 * represents information specific to the resource adapter for handling the
	 * connection request.
	 * 
	 * @param mcf
	 *            used by application server to delegate connection
	 *            matching/creation
	 * @param cxRequestInfo
	 *            connection request information
	 * 
	 * @return connection handle with an EIS specific connection interface
	 * 
	 * @exception ResourceException
	 *                if an error occurs
	 */
	public Object allocateConnection(ManagedConnectionFactory mcf,
			ConnectionRequestInfo cxRequestInfo) throws ResourceException {
		
		LOG.info("Allocating a connection");
		Object connection = null;
		
		ManagedConnection mc = mcf.createManagedConnection(null, cxRequestInfo);
		connection = mc.getConnection(null, cxRequestInfo);
		
		LOG.info("Connection allocated, type = " + connection.getClass().getName());
		return connection;
	}
}
